package com.lanou3g.exam.model;

import com.lanou3g.exam.pojo.User;
import com.lanou3g.exam.util.JDBCUitl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class LoginModel {
    //    public List<User>getUser(String username){
//        List<User>users=new ArrayList<>();
//        String sql="Select * from user where username like ?";
//        Connection conn =JDBCUitl.getConn();
//        try {
//            PreparedStatement statement =
//                    conn.prepareStatement(sql);
//            statement.setString(1,"%"+username+"%");
//            ResultSet rs=statement.executeQuery();
//            while (rs.next()){
//                User user =new User();
//                setUserParameter(user,rs);
//                users.add(user);
//                }
//                rs.close();
//            statement.close();
//        } catch (SQLException e) {
//            e.printStackTrace();
//        }
//        return users;
//    }
    public User login(String username, String pwd) {
        try {
            User user = new User();
            Connection conn = JDBCUitl.getConn();
            String sql = "select * from user where username= ? and pwd= ?";

            PreparedStatement statement
                    = conn.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, pwd);
            ResultSet rs = statement.executeQuery();
            if (rs.next()) {
                int count = rs.getInt(1);
                System.out.println(count);
                System.out.println(rs.getString("username"));
                String username1 = rs.getString("username");
                String pwd1 = rs.getString("pwd");
                User user1 = new User();
                user1.setUsername(username1);
                user1.setPwd(pwd1);
                return user1;
            }
//            setUserParameter(user,rs);
            rs.close();
            statement.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

//    private void setUserParameter(User user, ResultSet rs) throws SQLException {
//        String username =rs.getString("username");
//        String nickname=rs.getString("nickname");
//        user.setUsername(username)
//                .setNickname(nickname)
//                .setPwd("");

    //    }
    public boolean canLogin(String username, String pwd) {
        Connection conn = JDBCUitl.getConn();
        String sql = "Select count(*) from user where username= ? and pwd = ?";
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, username);
            statement.setString(2, pwd);
            ResultSet rs = statement.executeQuery();
            boolean canLogin = false;
            if (rs.next()) {
                int count = rs.getInt(1);
                canLogin = count > 0;
            }
            statement.close();
            rs.close();
            return canLogin;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
